package Sort;

import java.util.Arrays;

/**
 * 时间复杂度：最差O(n^2) 平均O(n^2) 最好O(n)
 * 空间复杂度：O(1)
 */
public class 冒泡排序 {
    public static void bubbleSort(int[] nums) {
        for (int i = nums.length - 1; i > 0; i--) {
            boolean flag = false;
            for (int j = 0; j < i; j++) {
                if (nums[j] > nums[j + 1]) {
                    int temp = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = temp;
                    flag = true;
                }
            }
            if (!flag) break;
        }
    }

    public static void main(String[] args) {
        int[] nums = new int[]{-5, 7, 1, 3, -7, 1, -3, -1, 10, 0, -4, 0, 5, 2, -3, 0, 9, 8};
        bubbleSort(nums);
        System.out.println(Arrays.toString(nums));
    }
}
